/** 
 * Project Name:model-common 
 * File Name:CalendarUtils.java
 * Date:2018年4月9日上午10:07:02
 * Copyright (c) 2024, lqq All Rights Reserved.
 * 
 */
package com.cd.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author lqq
 * date:
 */
public class CalendarUtils {
	public static enum CalendarFormat{
		YYYY_MM("yyyy-MM"),
		YYYY_MM_DD("yyyy-MM-dd"),
		YYYY_MM_DD_Hms("yyyy-MM-dd HH:mm:ss"),
		YYYY_MM_DD_Hmss("yyyy-MM-dd HH:mm:sss"),
		YYYYMM("yyyyMM"),
		YYYYMMDD("yyyyMMdd"),
		YYYYMMDDHHmmss("yyyyMMddHHmmss"),
		YYYYMMDDHHmmsss("yyyyMMddHHmmsss")
		;

	    private String format;
	    CalendarFormat(String format){
	    	this.format = format;
	    }
	    public String getFormat() {
	    	return this.format;
	    }
	}
    
    /**
     * Calendar 转 String
     * 
     * @param cal
     * @param format
     * @return
     */
    public static String CalendarToString(Calendar cal, CalendarFormat format) {
    	if(null == cal) {
    		return null;
    	}
    	if(null == format) {
    		format = CalendarFormat.YYYY_MM_DD_Hms;
    	}
    	SimpleDateFormat sdf = new SimpleDateFormat(format.getFormat());
    	String dateStr = sdf.format(cal.getTime());
    	return dateStr;
    }
    
    /**
     * String 转 Calendar
     * 
     * @param calendarStr
     * @param format
     * @return
     * @throws ParseException
     */
    public static Calendar StringToCalendar(String calendarStr, CalendarFormat format) throws ParseException {
    	if(null == calendarStr) {
    		return null;
    	}
    	if(null == format) {
    		throw new RuntimeException("CalendarFormat is null");
    	}
	    SimpleDateFormat sdf= new SimpleDateFormat(format.getFormat());
	    Date date =sdf.parse(calendarStr);
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar;
    }
    
    /**
     * 转换为数字(int)
     * 例如: 2020,12,9 转为 int:20201209
     * 
     * @param year
     * @param month<br>
	 *                  Month value is 0-based. e.g., 0 for January.
     * @param day
     * @return
     */
    public static int convertToNumber(int year, int month, int day) {
		Calendar dayTime = Calendar.getInstance();
		dayTime.set(year, month, day);
		int dayNum = convertToDayNumber(dayTime);
		return dayNum;
    }
    
    /**
     * 转换为数字(int)
     * 例如: 2020,9 转为 int:202009
     * 
     * @param year
     * @param month<br>
	 *                  Month value is 0-based. e.g., 0 for January.
     * @param
     * @return
     */
    public static int convertToNumber(int year, int month) {
		Calendar dayTime = Calendar.getInstance();
		dayTime.set(year, month, 1);
		int dayNum = convertToMonthNumber(dayTime);
		return dayNum;
    }

    public static int convertToNumberByWeek(int year, int week) {
		StringBuilder weekStr = new StringBuilder();
		weekStr.append(Integer.toString(year));
		if(week<10) {
			weekStr.append("0");
		}
		weekStr.append(Integer.toString(week));
		int weekNum = Integer.valueOf(weekStr.toString());
		return weekNum;
    }
    

    /**
     * 转换为日期 Calendar
     * 
     * @param year
     * @param month
	 *                  Month value is 0-based. e.g., 0 for January.
     * @param day
     * 如果day=0, 则 为上个月底, 例如: 2020,11(12月),0 输出: 2020-11-31
     * @return
     */
    public static Calendar convertToCalendar(int year, int month, int day) {
		Calendar dayTime = Calendar.getInstance();
		dayTime.set(year, month, day, 0, 0, 0);
		return dayTime;
    }

	/**
	 * 转换为年月日数字(int)
     * 例如: Calendar 转为 int:20200912
     * 
	 * @param cal
	 * @return
	 */
	public static int convertToDayNumber(Calendar cal) {
		String dayStr = CalendarUtils.CalendarToString(cal, CalendarFormat.YYYYMMDD);
		int dayNum = Integer.valueOf(dayStr);
		return dayNum;
	}
	
	/**
	 * 转换为年月数字(int)
     * 例如: Calendar 转为 int:202009
     * 
	 * @param cal
	 * @return
	 */
	public static int convertToMonthNumber(Calendar cal) {
		String dayStr = CalendarUtils.CalendarToString(cal, CalendarFormat.YYYYMM);
		int dayNum = Integer.valueOf(dayStr);
		return dayNum;
	}
	
	/**
	 * 获取当前时间天的开始,例如 2020-12-22 04:34:23 输出: 2020-12-22 00:00:00
	 * 
	 * @param cal
	 * @return
	 */
	public static Calendar getDayBegin(Calendar cal) {
		Calendar theCal = Calendar.getInstance();
		theCal.setTime(cal.getTime());
		theCal.set(Calendar.HOUR, 0);
		theCal.set(Calendar.MINUTE, 0);
		theCal.set(Calendar.SECOND, 0);
		theCal.set(Calendar.MILLISECOND,0);
		return theCal;
	}
	
	/**
	 * 获取当前时间天的结束,例如 2020-12-22 04:34:23 输出: 2020-12-22 23:59:59
	 * 
	 * @param cal
	 * @return
	 */
	public static Calendar getDayEnd(Calendar cal) {
		Calendar theCal = Calendar.getInstance();
		theCal.setTime(cal.getTime());
		theCal.set(Calendar.HOUR, 23);
		theCal.set(Calendar.MINUTE, 59);
		theCal.set(Calendar.SECOND, 59);
		//theCal.set(Calendar.MILLISECOND,999);
		theCal.set(Calendar.MILLISECOND,0);
		return theCal;
	}
	
	
	/**
	 * 随机日期
	 * 
	 * @param beginDate 格式: yyyy-MM-dd
	 * @param endDate 格式: yyyy-MM-dd
	 * @return
	 */
	public static Calendar randomCalendar(String beginDate, String endDate) {
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date start = format.parse(beginDate);
			Date end = format.parse(endDate);

			if (start.getTime() >= end.getTime()) {
				return null;
			}
			long date = random(start.getTime(), end.getTime());
			Calendar theCal = Calendar.getInstance();
			theCal.setTimeInMillis(date);
			return theCal;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	private static long random(long begin, long end) {
		long rtn = begin + (long) (Math.random() * (end - begin));
		if (rtn == begin || rtn == end) {
			return random(begin, end);
		}
		return rtn;
	}

	/**
	 * @param createdTimestamp
	 *            毫秒
	 * @return
	 */
	public static String timestampToString(long createdTimestamp, CalendarFormat format) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(createdTimestamp);
		return CalendarUtils.CalendarToString(cal, format);
	}
}
